<template>
  <div class='table'>
    <ui-container>
      <div style="font-size:12px;padding-left:15px;color:#999;margin-top:10px;">
        <span style="color:red;">填写格式提示：</span>
        <span>商城优惠券每天兑换次数上限、商城优惠券有效期天数请输入正整数；其它则最多小数点后2位</span>
      </div>
      <div class="rows" v-for="(itme,index) in dataList" :key="index">
        <div class="label">{{itme.KeyStr}}:</div>
        <div>
          <el-input
            placeholder=""
            v-model="itme.Value"
            size="small"
            clearable style="width:300px">
          </el-input>
        </div>
        <div>
          <el-button type="primary" size="small" @click="save(itme)">保存</el-button>
          <span style="color:red;margin-left:5px;" v-if="itme.Key==0" ><span >{{itme.Value}}</span>积分 = 价值1元的商城优惠券</span>
          <span style="color:red;margin-left:5px;" v-if="itme.Key==4" ><span >{{itme.Value}}</span>元账单金额 = 1积分</span>
        </div>
      </div>
    </ui-container>
  </div>
</template>
<script type='text/javascript'>
export default {
  name: 'IntegralTransfor',
  data () {
    return {
      dataList:null,
    }
  },
  mounted () {
    this.init();
  },
  methods: {
    init(){
      this.$api.user.getSystems().then((rs) => {
        if(rs.IsSuccess){
          // for(let i=0;i<rs.Data.length;i++){
          //   if(rs.Data[i].Key==0){
          //     let html = `<span>${rs.Data[i].Value}</span>积分 = 价值1元的商城优惠券`
          //     rs.Data[i].desc = html
          //   }
          //   if(rs.Data[i].Key==4){
          //     rs.Data[i].desc = 'N元账单金额 = 1积分'
          //   }
          // }
          this.dataList = rs.Data;
        }
      }).catch((res) =>{})
    },
    save(row){
      let data ={
        Key:row.Key,
        Value:row.Value,
        Remark:row.Remark
      }
      if(row.Value <= 0 ){
        this.$message({
            type: 'error',
            message: '请输入大于0的数值'
          });
        return;
      }
      if(row.Key == 1 || row.Key == 3 ){
        let r = /^\+?[1-9][0-9]*$/;　　//正整数
        if(!r.test(row.Value)){
          this.$message({
            type: 'error',
            message: '格式不正确，请输入正整数'
          });
        return;
        }
      }else{
        let myreg=/^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/;  
        if (!myreg.test(row.Value)) {  
             this.$message({
            type: 'error',
            message: '格式不正确，最多两位小数'
          });
            return false;
        }
      }
      this.$api.user.setSystems(data).then((rs) => {
        if(rs.IsSuccess){
          this.$message({
            type: 'success',
            message: '设置成功!'
          });
        }
      }).catch((res) =>{})
    }
  }
}
</script>
<style lang='scss' type='text/scss' rel='stylesheet/scss' scope>
.container-main{
  width: 100% !important;
}
.rows {
  font-size: 14px;
  color: #666;
  margin: 15px 0;
  display: flex;
  align-items: center;
  .label{
    min-width: 320px;
    text-align: right;
    margin-right: 10px;
  }
  button {
    margin-left: 10px;
  }
}
</style>
